<?php

/**
 * ShopEx licence
 * wxpapi_ctl_goodsOrder
 *
 * @uses wxapi
 * @uses _frontpage
 * @package
 * @version $id$
 * @copyright  Copyright (c) 2005-2013 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @author Tobias Schlitt <toby@php.net>
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
class wxapi_ctl_goodsCart extends wxapi_response
{

    /**
     * add
     *
     * @access public
     * @return void
     */
    public function add()
    {
        $product_id = floatval($_GET['goodsId']);
        $num        = floatval($_GET['num']);        
        
        $rs_product = app::get('b2c')->model('products')->getRow('goods_id,product_id,store', ['product_id'=>$product_id]);
        if($rs_product){
            $goods_id = $rs_product['goods_id'];
            $_POST = [
                'goods' => [
                    'goods_id' => $goods_id,
                    'product_id' => $product_id,
                    'num' => $num,
                ],
                0 => 'goods',
            ];
            kernel::single('wxapi_respositories_cart_cart')->add('goods');
            $api_data = [
                'result' => "0",
                'msg' => "",
                'code'=> 0,
            ];
            self::return_json($api_data);
        } else {
            self::api_error('商品不存在');
        }
        exit;
    }

    /**
     * cartList
     *
     * @access public
     * @return void
     */
    public function cartList()
    {
        $list = kernel::single('wxapi_respositories_cart_cart')->index();
        $cart_data = $this->formatCart($list);
        $api_data  = [
            'msg' => '',
            'code' => 0,
            'totalPrice' => number_format($list['aCart']['subtotal'], 2),
            'list' => $cart_data,
        ];
        self::return_json($api_data);
    }

    /**
     * delete
     *
     * @access public
     * @return void
     */
    public function cartDel()
    {
        $cart_ids = json_decode($_REQUEST['cartIdList'], true);
        $cart_id_str = $cart_ids[0];//goods_94_546 这种格式
        $cart_data_arr = explode('_', $cart_id_str);

        $_POST = [
            'obj_type' => $cart_data_arr[0],
            'goods_ident' => $cart_id_str,
            'goods_id' => floatval($cart_data_arr[1]),
            'modify_quantity' => [
                $cart_id_str => [
                    'quantity' => 1,
                ],
            ],
        ];

        kernel::single('wxapi_respositories_cart_cart')->remove('goods');
        
        $totalPrice = kernel::single('wxapi_respositories_cart_cart')->get_cart_price();
        
        echo json_encode( [
            'reason' => '',
            'code' => 0,
            'totalPrice' => number_format($totalPrice['subtotal'], 2),
        ]);
        exit;
    }
    
    //部分勾选
    function check()
    {
        $obj_ident = $_REQUEST['id'];//goods_94_546 这种格式
        $is_check = intval($_REQUEST['ischecked']);
        $member_id = $this->member_info['member_id'];
        $oCartObjects = app::get('b2c')->model('cart_objects');
        $oCartObjects->update(
            array('is_check' => $is_check),
            array(
                'obj_ident' => $obj_ident,
                'member_id' => $member_id,
            )
        );
        
        $totalPrice = kernel::single('wxapi_respositories_cart_cart')->get_cart_price();
        
        $api_data = array(
            'result' => '0',
            'reason' => '',
            'code' => '0',
            'totalPrice' => number_format($totalPrice['subtotal'], 2),
        );
        self::return_json($api_data);
    }
    
    //全选
    function checkAll()
    {
        $obj_ident = $_REQUEST['id'];//goods_94_546 这种格式
        $is_check = intval($_REQUEST['check']);
        $member_id = $this->member_info['member_id'];
        $oCartObjects = app::get('b2c')->model('cart_objects');
        $oCartObjects->update(
            array('is_check' => $is_check),
            array('member_id' => $member_id)
        );
        
        $totalPrice = kernel::single('wxapi_respositories_cart_cart')->get_cart_price();
        
        $api_data = array(
            'result' => '0',
            'reason' => '',
            'code' => '0',
            'totalPrice' => number_format($totalPrice['subtotal'], 2),
        );
        self::return_json($api_data);        
    }

    /**
     * updateNum
     *
     * @access public
     * @return void
     */
    public function updateNum()
    {
        $cart_id = $_REQUEST['id'];//goods_94_546 这种格式
        $num = intval($_REQUEST['num']);
        $cart_data_arr = explode('_', $cart_id);

        $_POST = [
            'obj_type' => $cart_data_arr[0],
            'goods_ident' => $cart_id,
            'goods_id' => $cart_data_arr[1],
            'modify_quantity' => [
                $cart_id => [
                    'quantity' => $num,
                ],
            ],
        ];

        kernel::single('wxapi_respositories_cart_cart')->update();
        
        $totalPrice = kernel::single('wxapi_respositories_cart_cart')->get_cart_price();
        
        $api_data = [
            'reason' => '',
            'code' => 0,
            'totalPrice' => number_format($totalPrice['subtotal'], 2),
        ];
        self::return_json($api_data);
        exit;
    }

    /**
     * formatCart
     *
     * @param mixed $cart
     * @access public
     * @return void
     */
    public function formatCart($cart)
    {
        $list = [];
        foreach($cart['aCart']['object']['goods'] as $goods)
        {
            $list[] = $this->formatGoods($goods);
        }
        return $list;
    }

    /**
     * formatGoods
     *
     * @param mixed $goods
     * @access public
     * @return void
     */
    public function formatGoods($goods)
    {
        //return $goods;
        return [
            "brandName"=>'',
            "buyEnable" =>1 ,
            "canWhole"=>true,
            "goodsId"=> $goods['obj_items']['products'][0]['goods_id'], //"2c9257a16136c3d60162e20b1fe74c20",
            "goodsName"=>$goods['obj_items']['products'][0]['name'],// : "碎花雪纺连衣裙 T18C075",
            "goodsSkuId"=>$goods['obj_items']['products'][0]['product_id'],// : "2c9257a16136c3d60162e20b20014c26",
            "goodsSkuValName"=>(string)$goods['obj_items']['products'][0]['spec_info'],//: "[\"M\"]",
            "goodsSkuVals"=>explode('、', $goods['obj_items']['products'][0]['spec_desc']), //: ["M"],
            "id"=> $goods['obj_ident'],
//          "id"=>$goods['obj_items']['products'][0]['product_id'], // : "2c9257a16136c3d60162e6c3e06f4d6a",
            "ischecked"=> $goods['is_check'] ? 1 : 0,
            "logo"=> base_storager::image_path($goods['obj_items']['products'][0]['thumbnail']),//: "http://sujiefs.com/upload/images/20180420/201804201549403948912.jpg",
            "minBuyNum"=> 1,
            "num"=>$goods['quantity'],//: 1,
            "orgPrice"=>$goods['obj_items']['products'][0]['price']['price'],//: "142",
            "price"=>round($goods['obj_items']['products'][0]['price']['price'], 2),//: "142",
            "priceSubtotal"=>$goods['obj_items']['products'][0]['price']['buy_price'],//: "142",
            "status"=> 1,//: 1,
            "stockNum"=> 20,//: 20,
            "thumLogo"=> base_storager::image_path($goods['obj_items']['products'][0]['thumbnail']),//: "http://sujiefs.com/upload/images/20180420/201804201549403948912_thumbnail.jpg",
            "type"=> 1, //: 1,
            "wholeNum"=>$goods['obj_items']['products'][0]['quantity'],// : ,
            "wholePrice"=>$goods['obj_items']['products'][0]['quantity'] * $goods['obj_items']['products'][0]['price']['price'] , //: 139
        ];

    }

}


